package com.wawei.seeds.services;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.wawei.seeds.adapter.WalnutMainActivityAdapter;
import com.wawei.seeds.model.Appointment;
import com.wawei.seeds.model.WalnutMainEvent;
import com.wawei.seeds.services.remote.AppointmentRestfulWrapper;
import com.wawei.seeds.services.remote.MainServiceRestfulWrapper;
import com.wawei.seeds.services.remote.ReservationServiceRestfulService;
import com.wawei.seeds.services.remote.WalnutMainServiceRestfulService;
import com.wawei.seeds.utils.NetBase;
import com.wawei.seeds.utils.PrefUtils;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutNotifyHandler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 *
 * Created by t27891 on 15/6/22.
 */
public class WalnutMyServiceServiceHandler implements ServiceHandler {
    private Context context;

    public WalnutMyServiceServiceHandler(Context context) {
        this.context = context;
    }

    @Override
    public void retrieveData(RestAdapter restAdapter, Bundle bundle, final Handler handler, final Integer handlerTag) {
        List<WalnutMainEvent> items = new ArrayList<>(0);
        WalnutMainEvent item;
        switch (handlerTag.intValue()) {
            case WalnutNotifyHandler.LOAD_NEW_ACHIEVE_DATA:
                RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                break;
            case WalnutNotifyHandler.LOAD_OLD_ACHIEVE_DATA:
                RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                break;
            case WalnutNotifyHandler.INIT_ACHIEVE_DATA:
                WalnutMainServiceRestfulService restfulService = restAdapter.create(WalnutMainServiceRestfulService.class);

                String useId = String.valueOf(PrefUtils.getUserId(context));
                restfulService.getMyWanlutServices(useId, new Callback<MainServiceRestfulWrapper>() {
                    @Override
                    public void success(MainServiceRestfulWrapper restfulWrapper, Response response) {
                        /*
                        List<Appointment> appointments = restfulWrapper.getBookingBeans();
                        List<WalnutMainEvent> events = null;
                        for(int i=0; null != appointments && i < appointments.size();i++){
                            Appointment appointment = appointments.get(i);
                            if(null != appointment.items){
                                for(WalnutMainEvent mainEvent : appointment.items){
                                    mainEvent.publisherHeadPortrait = (null == appointment.user)?null:appointment.user.headPortrait;
                                    mainEvent.bookingState = appointment.bookingState;
                                }
                                if(null == events) {
                                    events = appointment.items;
                                }
                                else{
                                    events.addAll(appointment.items);
                                }
                            }
                            else if (appointment.items != null){
                                events.addAll(appointment.items);
                            }
                        }
                        */
                        List<WalnutMainEvent> events = restfulWrapper.getItemBeans();
                        if(null != events){
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, events.toArray());
                        }
                        else {
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                        }
                    }

                    @Override
                    public void failure(RetrofitError retrofitError) {
                        RetrofitNetworkService.handleError(handler,handlerTag, retrofitError);
                    }
                });
                break;
            default:
                break;
        }
    }

    public void retrieveAppointmentManList(RestAdapter restAdapter, String eventId, final Handler handler, final Integer handlerTag) {
        WalnutMainServiceRestfulService restfulService = restAdapter.create(WalnutMainServiceRestfulService.class);
        restfulService.getMyServiceDetail(eventId, new Callback<AppointmentRestfulWrapper>() {
            @Override
            public void success(AppointmentRestfulWrapper appointmentRestfulWrapper, Response response) {
                try {
                    List<Appointment> bookingBeans = appointmentRestfulWrapper.getBookingBeans();
                    if (bookingBeans != null && !bookingBeans.isEmpty()) {
                        List<WalnutMainActivityAdapter.WalnutEventAppointmentMan> items = new ArrayList<>(0);
                        for (Appointment entity : bookingBeans) {
                            WalnutMainActivityAdapter.WalnutEventAppointmentMan item = new WalnutMainActivityAdapter.WalnutEventAppointmentMan();
                            item.bookingId = entity.bookingId;
                            item.userId = entity.userId;
                            item.phone = entity.user == null ? null : entity.user.mobile;
                            item.userName = entity.user == null ? null : entity.user.userName;
                            item.headPortrait = entity.user == null ? null : entity.user.headPortrait;
                            item.appointmentStatus = entity.bookingState;
                            item.appointmentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date(1000 * entity.applyTime));
                            items.add(item);
                        }
                        WalnutMainEvent event = new WalnutMainEvent();
                        event.bookerList.addAll(items);
                        RetrofitNetworkService.handleSuccess(handler, handlerTag, event);
                    } else {
                        RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                    }
                } catch (Exception e) {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, error);
            }
        });
    }

    public void acceptBooking(RestAdapter restAdapter, final String bookId, final Handler handler, final Integer handlerTag) {
        ReservationServiceRestfulService restfulService = restAdapter.create(ReservationServiceRestfulService.class);
        restfulService.acceptBooking(bookId, new Callback<NetBase>() {

            @Override
            public void success(NetBase netBase, Response response) {
                if (netBase.getResultCode() == RetrofitNetworkService.SUCCESS) {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, bookId);
                } else {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, error);
            }
        });
    }

    public void cancelBooking(RestAdapter restAdapter, final String bookId, final Handler handler, final Integer handlerTag) {
        ReservationServiceRestfulService restfulService = restAdapter.create(ReservationServiceRestfulService.class);
        restfulService.cancelBooking(bookId, new Callback<NetBase>() {

            @Override
            public void success(NetBase netBase, Response response) {
                if (netBase.getResultCode() == RetrofitNetworkService.SUCCESS) {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, bookId);
                } else {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, error);
            }
        });
    }
}
